﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace CLZ.Common
{
    public class KVBCommon
    {
        /// <summary>
        /// 处理以逗号隔开的ID字符串
        /// </summary>
        /// <param name="IDList">ID 数组</param>
        /// <returns></returns>
        public static string GetIDListString(int[] IDList)
        {
            string returnString = string.Empty;

            if (IDList.Length > 0)
            {
                returnString = "0";
            }

            foreach (int id in IDList)
            {
                if (id > 0)
                {
                    returnString = returnString + "," + id.ToString();
                }
            }

            if (IDList.Length > 0)
            {
                returnString =  returnString + ",0";
            }

            return returnString;
        }

        /// <summary>
        /// 处理以逗号隔开的ID字符串
        /// </summary>
        /// <param name="IDListStr">ID字符串</param>
        /// <returns></returns>
        public static string GetIDListString(string IDListStr)
        {
            string returnString = string.Empty;

            if (IDListStr.Trim().Length > 0)
            {
                string[] IDList = IDListStr.Split(',');

                if (IDList.Length > 0)
                {
                    returnString = "0";
                }

                foreach (string idStr in IDList)
                {
                    int id = 0;
                    int.TryParse(idStr, out id);

                    if (id > 0)
                    {
                        returnString = returnString + "," + id.ToString();
                    }
                }

                if (IDList.Length > 0)
                {
                    returnString = returnString + ",0";
                }
            }

            return returnString;
        }


        /// <summary>
        /// 生成登录帐号
        /// </summary>
        /// <returns></returns>
        public static string CreateLoginID(string prefixStr, int length)
        {
            StringBuilder loginID = new StringBuilder();
            if (!string.IsNullOrEmpty(prefixStr))
            {
                loginID.Append(prefixStr);
            }
            Random random = new Random();
            string strLetters = "1234567890";
            for (int i = 0; i < length; i++)
            {
                loginID.Append(strLetters.Substring(random.Next(0, strLetters.Length - 1), 1));
            }
            return loginID.ToString();
        }

        /// <summary>
        /// 生成密码
        /// </summary>
        /// <returns></returns>
        public static string CreatePassword(int length)
        {
            StringBuilder passwrod = new StringBuilder();
            Random r = new Random();
            string strLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
            for (int i = 0; i < length; i++)
            {
                passwrod.Append(strLetters.Substring(r.Next(0, strLetters.Length - 1), 1));
            }
            return passwrod.ToString();
        }

        #region Delete File
        public static bool DeleteFile(string FilePath)
        {
            bool returnStatus = false;

            try
            {
                string path = System.Web.HttpContext.Current.Server.MapPath(FilePath);
                if (File.Exists(path))
                {
                    File.Delete(path);
                }

                returnStatus = true;
            }
            catch { }

            return returnStatus;
        }
        #endregion
    }
}
